package com.linxiao.hrcrm.sms.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.exception.UtilException;
import com.ruoyi.common.utils.sign.Md5Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 *
 * 大汉短信发送
 * @author zhoudong
 *
 */
public class DahanSms {

    private static final Logger logger = LoggerFactory.getLogger(DahanSms.class);

    // 测试账号密码
    private static final String NOTICE_ACCOUNT = "";
    private static final String NOTICE_PASSWORD = "";

    /**
     * 短信发送地址
     */
    private static final String URL = "https://www.dh3t.com/json/sms/Submit";

    /**
     * 发送短信
     * @param mobiles 手机号
     * @param content 内容
     * @return
     */
    public static String send(List<String> mobiles, String content) {
        if (CollUtil.isEmpty(mobiles) || StrUtil.isBlank(content)) {
            logger.error("手机号或短信内容不能为空");
            throw new UtilException("手机号或短信内容不能为空");
        }

        // 组装参数
        String phones = String.join(",", mobiles);

        Map<String, Object> map = Map.of(
                "account", NOTICE_ACCOUNT,
                "password", Md5Utils.hash(NOTICE_PASSWORD),
                "phones", phones,
                "content", content);

        return HttpUtil.post(URL, JSON.toJSONString(map));
    }

    /**
     * 发送短信
     * @param mobile 手机号
     * @param content 内容
     * @return
     */
    public static String send(String mobile, String content) {
        if (StrUtil.isBlank(mobile) || StrUtil.isBlank(content)) {
            logger.error("手机号或短信内容不能为空");
            throw new UtilException("手机号或短信内容不能为空");
        }
        return send(List.of(mobile), content);
    }
}
